﻿

Partial Class pages_emailTemplates_Preview
    Inherits CPage

#Region "Querystring"
    Public ReadOnly Property TemplateId() As Integer
        Get
            Return CWeb.RequestInt("templateId")
        End Get
    End Property
    Public ReadOnly Property ProductId() As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
    Public ReadOnly Property CustomerId() As Integer
        Get
            Return CWeb.RequestInt("customerId")
        End Get
    End Property
    Public ReadOnly Property IsPreview() As Boolean
        Get
            Return CWeb.RequestBool("preview")
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [EmailTemplate]() As CEmailTemplate
        Get
            Return CEmailTemplate.Cache.GetById(TemplateId)
        End Get
    End Property
    Public ReadOnly Property Product As CProduct
        Get
            If ProductId > 0 And Not IsPostBack Then Return CProduct.Cache.GetById(ProductId)
            Dim p As CProduct = CProduct.Cache.GetById(CDropdown.GetInt(ddProduct))
            If IsNothing(p) Then
                If CProduct.Cache.Count > 0 Then
                    p = CProduct.Cache(0)
                    If TemplateId = EEmailTemplate.Product_Info_ConferenceSet AndAlso CProduct.Cache.ConferenceSets.Count > 0 Then
                        p = CProduct.Cache.ConferenceSets(0)
                    End If
                Else
                    p = New CProduct
                    p.ProductName = "Northwind"
                    p.Button = CButton.Cache(0)
                    p.ProductDescription = "description description description description description description"
                End If
            End If
            Return p
        End Get
    End Property
    Public ReadOnly Property Customer As CCustomer
        Get
            Dim c As New CCustomer()
            If CustomerId > 0 Then
                c = New CCustomer(CustomerId)
            Else
                c.CustomerName = "FirstName LastName"
                c.CustomerEmail = "Email@test.com"
            End If
            Return c
        End Get
    End Property
#End Region


#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()

        MenuSelected = "Email Templates"
        AddMenuSide("Email Templates...", CSitemap.EmailTemplates(String.Empty, Not EmailTemplate.TemplateHasSubject))
        AddMenuSide("Template Details", CSitemap.EmailTemplateEdit(TemplateId))
        AddMenuSide("Preview")

        ddTemplate.DataSource = CEmailTemplate.Cache
        ddTemplate.DataBind()
        CDropdown.SetValue(ddTemplate, TemplateId)

        ddProduct.DataSource = CProduct.Cache.GetByTypeId(EProductType.ConferenceSet)
        If EmailTemplate.TemplateProductTypeId > 0 Then
            ddProduct.DataSource = CProduct.Cache.GetByTypeId(EmailTemplate.TemplateProductTypeId)
        End If
        ddProduct.DataBind()
        ddProduct.SelectedIndex = 0

        ddCustomer.DataSource = New CCustomer().SelectAll(New CPagingInfo(100))
        ddCustomer.DataBind()
        ddCustomer.SelectedIndex = 0

        txtEmail.Text = Request("MyAdminEmail")


        If IsPreview Then
            PreviewTemplate()
            Response.End()
        End If
    End Sub
    Protected Overrides Sub PagePreRender()
        iframe.Attributes.Add("src", String.Concat("?templateId=", TemplateId, "&preview=true&productId=", CDropdown.GetInt(ddProduct), "&customerId=", CDropdown.GetInt(ddCustomer)))

        Dim p As CProduct = Me.Product
        Dim c As CCustomer = Me.Customer
        txtSubject.Text = EmailTemplate.ProcessSubject(p, c)

        txtEmail.Text = Request("MyAdminEmail")

        If Not IsNothing(EmailTemplate) AndAlso Not EmailTemplate.TemplateHasSubject Then
            txtSubject.Visible = False
            txtEmail.Visible = False
        End If
    End Sub
#End Region


#Region "Event Handlers - Form"
    Protected Sub ddTemplate_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddTemplate.SelectedIndexChanged
        Response.Redirect(CSitemap.EmailTemplatePreview(CDropdown.GetInt(ddTemplate)))
    End Sub
#End Region

#Region "Private"
    Private Sub PreviewTemplate()
        Dim p As CProduct = Me.Product
        Dim c As CCustomer = Me.Customer
        Dim o As New COrder 'For paypal: ProductButtonId/OrderId/CustomerEmail
        o.Product = p
        o.Customer = c

        'Special cases
        If TemplateId = EEmailTemplate.Files_Item AndAlso CFile.Cache.Count > 0 Then
            Dim f As CFile = CFile.Cache(0)
            Response.Write(EmailTemplate.ProcessFile(f, o, Nothing, Nothing, Nothing))
            Exit Sub
        End If

        Dim body As String = EmailTemplate.Process(o, Nothing, 0)
        Response.Write(body)
    End Sub
#End Region


    Protected Sub btnSend_Click(sender As Object, e As System.EventArgs) Handles btnSend.Click
        Dim p As CProduct = Me.Product
        Dim c As CCustomer = Me.Customer
        Dim o As New COrder 'For paypal: ProductButtonId/OrderId/CustomerEmail
        o.Customer = c
        o.Product = p

        txtSubject.Text = EmailTemplate.ProcessSubject(p, c)

        'CConfig.SendMail(txtEmail.Text, txtSubject.Text, EmailTemplate.Process(p, c))

        Dim msg As New Net.Mail.MailMessage()
        With msg
            .To.Add(New Net.Mail.MailAddress(txtEmail.Text))
            .Subject = txtSubject.Text
            .Body = EmailTemplate.Process(o, msg, 0)
            .IsBodyHtml = True
        End With

        'CConfig.Gmail.Send(msg)
        With New Net.Mail.SmtpClient
            .Send(msg)
        End With

        Response.Cookies.Add(New HttpCookie("MyAdminEmail", txtEmail.Text))
    End Sub
End Class
